package com.rs.controller.verify;

import com.rs.cache.LocalCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.locks.LockSupport;

/**
 * 检查是否可继续操作,模拟第三方验证操作和定时任务
 *
 * @author zhouwz
 * @date 2020/6/13 6:42
 */
@RestController
@Slf4j
public class VerifyContinueController {

    /**
     * 唤醒对应线程
     *
     * @param vehicleCode 车辆编号
     */
    @GetMapping("/continueVehicle")
    public void continueVehicle(@RequestParam("vehicleCode") String vehicleCode) {
        LockSupport.unpark(LocalCache.vehicleLock.get(vehicleCode));
        LocalCache.vehicleLock.remove(vehicleCode);
        log.info("车辆:{},解锁成功,还剩余{}车被锁!", vehicleCode, LocalCache.vehicleLock.size());
    }

    /**
     * 唤醒全部线程
     */
    @GetMapping("/continueAll")
    public void continueAll() {
        for (String vehicle : LocalCache.vehicleLock.keySet()) {
            LockSupport.unpark(LocalCache.vehicleLock.get(vehicle));
        }
    }
}
